﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;

namespace Projekt_GUI.Forms
{
    public partial class Einkaufswagen : Form
    {
        OracleHelper oracle { get { return OracleHelper.Instance; } }
        public Einkaufswagen()
        {
            InitializeComponent();
        }

        private void Einkaufswagen_Load(object sender, EventArgs e)
        {
            Refresh();
        }

        private void Refresh()
        {
            listView1.Items.Clear();
            int ObjectCouter = 0;
            float Summe = 0;
            foreach (System.Collections.Generic.KeyValuePair<string, string> s in Form1.Cart)
            {
                string Preis = "0", Name="";
                oracle.Open();
                OracleDataReader r = oracle.Select("Beschreibung, Preis", "Reisen", "Nummer = "+s.Value); 
                while (r.Read())
                {
                    Name = r.GetValue(0).ToString();
                    Preis = r.GetValue(1).ToString();
                }
                Summe += float.Parse(Preis);
                listView1.Items.Add(Name);
                listView1.Items[ObjectCouter].SubItems.Add("Reise");
                listView1.Items[ObjectCouter].SubItems.Add(Preis + "€");
                listView1.Items[ObjectCouter].SubItems.Add(s.Key);
                ObjectCouter++;
                oracle.Close();
            }

            foreach (System.Collections.Generic.KeyValuePair<string, string> s in Form1.CartExt)
            {
                string Preis = "0", Name = "";
                    oracle.Open();
                    OracleDataReader r = oracle.Select("Name, Preis", "Erweiterungen", "Nummer = " + s.Value);

                    while (r.Read())
                    {
                        Name = r.GetValue(0).ToString();
                        Preis = r.GetValue(1).ToString();
                    }
                Summe += float.Parse(Preis);
                listView1.Items.Add(Name);
                listView1.Items[ObjectCouter].SubItems.Add("Erweiterungen");
                listView1.Items[ObjectCouter].SubItems.Add(Preis + "€");
                listView1.Items[ObjectCouter].SubItems.Add(s.Key);
                ObjectCouter++;
                oracle.Close();
            }

            toolStripButton4.Text = Summe.ToString("F2") +"€";
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            Refresh();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            Erweiterungen ew = new Erweiterungen();
            ew.ShowDialog();
            Refresh();
        }
        
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("Wollen Sie wirklich den Warenkorb löschen?", "Warnung", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (res == System.Windows.Forms.DialogResult.Yes)
            {
                Form1.Cart = new Dictionary<string, string>();
                Form1.CartExt = new Dictionary<string, string>();
                listView1.Clear();
                Refresh();
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                if (listView1.SelectedItems[0].SubItems[1].Text == "Reise")
                    Form1.Cart.Remove(listView1.SelectedItems[0].SubItems[3].Text);
                if (listView1.SelectedItems[0].SubItems[1].Text == "Erweiterungen")
                    Form1.CartExt.Remove(listView1.SelectedItems[0].SubItems[3].Text);

                listView1.Items.Clear();
                Refresh();
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Buchung_SelectKunde sk = new Buchung_SelectKunde();
            sk.ShowDialog();
            if (Form1.KundenID != "")
            {
                BuchungToDo td = new BuchungToDo();
                td.ShowDialog();

                Form1.Cart = new Dictionary<string, string>();
                Form1.CartExt = new Dictionary<string, string>();
                listView1.Clear();
                Refresh();

            }
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {

        }
    }
}
